# Compare the emotional reactions of the top two parties in presidential elections and 
# the top two parties in legislative elections. See panel B of Table S.1, Appendix S.




#############
# load data #
#############

library(stargazer)

# country level data
load("PSRM Replication Files/PresidentialCountries.RData")

colnames(presidential_country_df)

table(presidential_country_df$system)
# Directly Elected Presidential System    Other 
#                                   48       32

# create new variables
presidential_country_df$loggdp <- log(presidential_country_df$gdp)
presidential_country_df$loggdppc <- log(presidential_country_df$gdppc)
presidential_country_df$logaverage_total <- log(presidential_country_df$average_total)
presidential_country_df$average_love <- 100 * presidential_country_df$average_love
presidential_country_df$average_angry <- 100 * presidential_country_df$average_angry




# presidential systems vs. other systems t tests
(gdp_t <- t.test(presidential_country_df$loggdp ~ presidential_country_df$system))
(gdppc_t <- t.test(presidential_country_df$loggdppc ~ presidential_country_df$system))
(ed_t <- t.test(presidential_country_df$electoral_democracy ~ presidential_country_df$system))
(ld_t <- t.test(presidential_country_df$liberal_democracy ~ presidential_country_df$system))
(ec_t <- t.test(presidential_country_df$economic_variance ~ presidential_country_df$system))
(so_t <- t.test(presidential_country_df$social_variance ~ presidential_country_df$system))
(plm_t <- t.test(presidential_country_df$average_populism ~ presidential_country_df$system))
(react_t <- t.test(presidential_country_df$logaverage_total ~ presidential_country_df$system))
(love_t <- t.test(presidential_country_df$average_love ~ presidential_country_df$system))
(angry_t <- t.test(presidential_country_df$average_angry ~ presidential_country_df$system))

# make table
colnames(presidential_country_df)[c(13, 14, 5, 6, 9, 7, 8, 15, 11, 12)]
#[1] "loggdp"              "loggdppc"            "electoral_democracy" "liberal_democracy"   "average_populism"   
#[6] "economic_variance"   "social_variance"     "logaverage_total"    "average_love"        "average_angry"

clocs <- c(13, 14, 5, 6, 9, 7, 8, 15, 11, 12)

p1tab <- as.data.frame(matrix(NA, nrow = 10, ncol = 5))
colnames(p1tab) <- c("variable", "mean1", "mean2", "p", "ci")

for(i in 1:length(clocs)){
  p1tab$variable[i] <- colnames(presidential_country_df)[clocs[i]]
  
  tt <- t.test(presidential_country_df[,clocs[i]] ~ presidential_country_df$system)
  
  est <- tt$estimate
  p1tab$mean1[i] <- est[1]
  p1tab$mean2[i] <- est[2]
  
  p1tab$p[i] <- tt$p.value
  
  p1tab$ci[i] <- paste0("[", paste(round(tt[[4]], 2), collapse = ", "), "]")
}

p1tab$difference <- paste0(round(p1tab$mean1 - p1tab$mean2, 2), ifelse(p1tab$p < 0.05, "*", ""))
p1tab$mean1 <- round(p1tab$mean1, 2)
p1tab$mean2 <- round(p1tab$mean2, 2)

# table S.2
stargazer(t(t(p1tab[,c(1:3,6,5)])),
          covariate.labels = c("Variable", "Directly Elected Presidential Systems", 
                               "Other Systems Mean", "Difference"))

# list of countries by system
paste(sort(presidential_country_df$country[which(presidential_country_df$system != "Other")]), collapse = ", ")
paste(sort(presidential_country_df$country[which(presidential_country_df$system == "Other")]), collapse = ", ")
